import { defineStore } from 'pinia'
import { find } from 'lodash'

interface CurrentPath { path: string, parentPath: string }
type OpenList = CurrentPath & { title?: string }
export const useTabCacheStore = defineStore(
  'tabCache',
  () => {
    const currentPath = ref({
      // 当前选中菜单的状态
      path: '', // 子菜单
      parentPath: '' // 父菜单
    })
    // 修改当前打开的页面记录
    const setCurrentPath = (_currentPath: CurrentPath) => {
      currentPath.value = _currentPath
    }

    /*
    {
        path: '',
        parentPath: '',
        title: '',
      },
    */
    //  记录用户操作过的页面
    const openList = ref<OpenList[]>([])
    const addOpenList = (_openList: OpenList) => {
      // const arr = [
      //   { id: 1, title: '111', name: 11 },
      //   { id: 1, title: '111' },
      //   { id: 2, title: 2222 },
      // ]
      // console.log(find(arr, { id: 1, title: '111', name: 11 }))
      if (!find(openList.value, { path: _openList.path })) {
        // 通过lodash 查找 列表是否有 此条件，没有才添加
        openList.value.push(_openList)
      }
    }

    return {
      currentPath,
      setCurrentPath,
      openList,
      addOpenList
    }
  },
  {
    persist: true
  }
)
